package ch.tbz.M326.FileSearch;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;

import javax.swing.plaf.metal.MetalIconFactory.FolderIcon16;

/**
 * 
 * @author fabs
 *
 */

public class FileSearcher {
	
	private Vector<File> foundFilesVector = new Vector<File>();


	
//	private void validateFile(File dir){
//		File[] files = dir.listFiles();
//		
//		if (files != null) {
//			for (int i = 0; i < files.length; i++) {
//				if (files[i].isDirectory()) {
//					validateFile(files[i]); 
//				}else{
//					if(files[i].getName().endsWith(".mp3")){
//						setFile(files[i]);
//					}
//				}
//			}
//		}
//	}
	
	private void searchFiles(File directory){
		File[] filesInDirectory = directory.listFiles();
		
		for(int i = 0; i < filesInDirectory.length; i++){
			if (!filesInDirectory[i].isDirectory() && filesInDirectory[i].getName().endsWith(".mp3")) {
				setFile(filesInDirectory[i]);
			}else if(filesInDirectory[i].isDirectory()){
//				System.out.println("SEARCH NEW FOLDER ["+filesInDirectory[i].getAbsolutePath()+"]");
				searchFiles(filesInDirectory[i]);
			}
		}
	}

	private void setFile(File file){
//		System.out.println(file.getName());
		foundFilesVector.add(file);
	}
	
	public Vector<File> getFiles(String location){
		File file = new File(location);
		
		searchFiles(file);
		return foundFilesVector;
	}	
}
